﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;


namespace gradeManager
{
    public partial class Mainform : Form
    {
        AutoAdaptWindowsSize autoAdaptSize;

        public Mainform()
        {
            InitializeComponent();
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

    private void button1_Click(object sender, EventArgs e)
        {
            //dataGridView1.Top = 25;
            //dataGridView1.Left = 30;
            //dataGridView1.Height = 350;
            //dataGridView1.Rows.Clear();//初始化
            dataGridView1.Visible = true;
            LinkSqLiteDB db = new LinkSqLiteDB();
            DataSet ds = new DataSet();
            if (db.DBconn())
            {
                string xx = LoginForm.StudentId;

               
                string sql = "select * from score_info where stuid='" + LoginForm.StudentId + "' and stuname ='" + textBox3.Text + "'";
                ds = db.GetScoreDs(sql);
                dataGridView1.DataSource = ds.Tables[0];
                dataGridView1.Update();
                //db.GetScoreData("select * from score_info where stuid='" + textBox1.Text + " and stuname =" + textBox3.Text +"'"); //执行sql
                //for (int i = 0; i < ScoreInfo.id.Count; i++)    //循环将数据实体类的数据存放到dataGridView中
                //{
                //    int index = this.dataGridView1.Rows.Add();
                //    this.dataGridView1.Rows[index].Cells[0].Value = ScoreInfo.id[i];        //填充
                //    this.dataGridView1.Rows[index].Cells[1].Value = ScoreInfo.stuid[i];   
                //    this.dataGridView1.Rows[index].Cells[2].Value = ScoreInfo.semester[i];    
                //    this.dataGridView1.Rows[index].Cells[3].Value = ScoreInfo.numofcourse[i];   
                //    this.dataGridView1.Rows[index].Cells[4].Value = ScoreInfo.nameofcourse[i];   
                //    this.dataGridView1.Rows[index].Cells[5].Value = ScoreInfo.teacher[i];
                //    this.dataGridView1.Rows[index].Cells[6].Value = ScoreInfo.credit[i];
                //    this.dataGridView1.Rows[index].Cells[7].Value = ScoreInfo.grade[i];
                //    this.dataGridView1.Rows[index].Cells[8].Value = ScoreInfo.stuname[i];
                //}
            }
            db.DBclose();

            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                //获取学分
                var dt = ds.Tables[0];
                double jd = 0;
                //int sum_score = 0;
                double sum_credit = 0;
                double sum_sc = 0;//学分绩
               
                for (int i = 0; i < dt.Rows.Count; i++)
                {
               
                    var score = Convert.ToInt32(dt.Rows[i]["grade"].ToString());
                    var credit = Convert.ToInt32(dt.Rows[i]["credit"].ToString());
                   
                    if (score >= 90 && score <= 100)
                    {
                        jd = 4;

                    }
                    if (score >= 85 && score <= 89)
                    {
                        jd = 3.7;
                    }

                    if (score >= 82 && score <= 84)
                    {
                        jd = 3.3;

                    }
                    if (score >= 78 && score <= 81)
                    {
                        jd = 3.0;

                    }
                    if (score >= 75 && score <= 77)
                    {
                        jd = 2.7;

                    }
                    if (score >= 72 && score <= 74)
                    {
                         jd = 2.3;

                    }
                    if (score >= 68 && score <= 71)
                    {
                        jd = 2.0;

                    }
                    if (score >= 64 && score <= 67)
                    {
                        jd = 1.5;

                    }
                    if (score >= 60 && score <= 63)
                    {
                       jd = 1.0;

                    }
                    if (score < 60)
                    {
                        jd = 0;
                    }

                    sum_sc += credit * jd;
                    sum_credit += credit;
                }
                label2.Text = "平均学分绩点为：" + Math.Round(sum_sc / sum_credit, 2);
               // MessageBox.Show(sum_credit.ToString());

            }

           

        }

        private double ExecuteGPA()
        {
            return 0;
        }
        /* 
         public double gpa(stuid)
         {

         }*/

        

        private void 主菜单_Load(object sender, EventArgs e)
        {
            dataGridView1.Visible = false;//隐藏
            //button1.Enabled = false;//查询按钮暂时不可以点击
           




        }
        //退出
        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        
        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }
        //添加
        private void button3_Click(object sender, EventArgs e)
        {
            AddForm tj = new AddForm(LoginForm.StudentId, textBox3.Text);
            //tj.txt_stuid = textBox1.Text;
            //tj.txt_stuid = LoginForm.StudentId;
            //tj.txt_stuname = textBox3.Text;
            tj.Show();

        }

        private void 主菜单_Shown(object sender, EventArgs e)
        {
           
            
        }
        public void deleteScore(string stuid, string numofcourse)
        {
            LinkSqLiteDB db = new LinkSqLiteDB();   //创建数据库连接对象
            if (db.DBconn())
            {
                string str = "DELETE FROM score_info WHERE stuid='" + stuid + "' and numofcourse='" + numofcourse+"'";
                string info = "删除成功！";

                if (db.UpdataDeletAdd(str))
                {
                    DialogResult dr = MessageBox.Show(info, "标题", MessageBoxButtons.OK, MessageBoxIcon.Question);
                    //if (dr == DialogResult.OK)
                    //{
                    //    //this.Visible = false;//隐藏当前窗口
                    //}
                    dataGridView1.Rows.Remove(dataGridView1.SelectedRows[0]);
                }
            }
            db.DBclose();//关闭数据连接
        }


        private void button4_Click(object sender, EventArgs e)
        {
             deleteScore(dataGridView1.SelectedRows[0].Cells["stuid"].Value.ToString(), 
                 dataGridView1.SelectedRows[0].Cells["numofcourse"].Value.ToString());
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }
        private void dataGridView1_Resize(object sender, EventArgs e)
        {

        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            if (autoAdaptSize != null)
            {
                autoAdaptSize.FormSizeChanged();
            }

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }

    public class AutoAdaptWindowsSize
    {
        double formOriginalWidth;//窗体原始宽度
        double formOriginalHeight;//窗体原始高度
        double scaleX;//水平缩放比例
        double scaleY;//垂直缩放比例
        Dictionary<string, string> ControlsInfo = new Dictionary<string, string>();//控件中心Left,Top,控件Width,控件Height,控件字体Size

        private Form _form;
        public AutoAdaptWindowsSize(Form form)
        {
            _form = form;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ctrlContainer">panel 控件</param>
        public void InitControlsInfo(Control ctrlContainer)
        {
            if (ctrlContainer.Parent == _form)//获取窗体的高度和宽度
            {
                formOriginalWidth = Convert.ToDouble(ctrlContainer.Width);
                formOriginalHeight = Convert.ToDouble(ctrlContainer.Height);
            }
            foreach (Control item in ctrlContainer.Controls)
            {
                if (item.Name.Trim() != "")
                {
                    //添加信息：键值：控件名，内容：据左边距离，距顶部距离，控件宽度，控件高度，控件字体。
                    ControlsInfo.Add(item.Name, (item.Left + item.Width / 2) + "," + (item.Top + item.Height / 2) + "," + item.Width + "," + item.Height + "," + item.Font.Size);
                }
                if ((item as UserControl) == null && item.Controls.Count > 0)
                {
                    InitControlsInfo(item);
                }
            }

        }
        public void FormSizeChanged()
        {
            if (ControlsInfo.Count > 0)//如果字典中有数据，即窗体改变
            {
                ControlsZoomScale(_form.Controls[0]);//表示pannel控件
                ControlsChange(_form.Controls[0]);

            }
        }
        private void ControlsZoomScale(Control ctrlContainer)
        {
            scaleX = (Convert.ToDouble(ctrlContainer.Width) / formOriginalWidth);
            scaleY = (Convert.ToDouble(ctrlContainer.Height) / formOriginalHeight);
        }
        /// <summary>
        /// 改变控件大小
        /// </summary>
        /// <param name="ctrlContainer"></param>
        private void ControlsChange(Control ctrlContainer)
        {
            double[] pos = new double[5];//pos数组保存当前控件中心Left,Top,控件Width,控件Height,控件字体Size
            foreach (Control item in ctrlContainer.Controls)//遍历控件
            {
                if (item.Name.Trim() != "")//如果控件名不是空，则执行
                {
                    if ((item as UserControl) == null && item.Controls.Count > 0)//如果不是自定义控件
                    {
                        ControlsChange(item);//循环执行
                    }
                    string[] strs = ControlsInfo[item.Name].Split(',');//从字典中查出的数据，以‘，’分割成字符串组

                    for (int i = 0; i < 5; i++)
                    {
                        pos[i] = Convert.ToDouble(strs[i]);//添加到临时数组
                    }
                    double itemWidth = pos[2] * scaleX;     //计算控件宽度，double类型
                    double itemHeight = pos[3] * scaleY;    //计算控件高度
                    item.Left = Convert.ToInt32(pos[0] * scaleX - itemWidth / 2);//计算控件距离左边距离
                    item.Top = Convert.ToInt32(pos[1] * scaleY - itemHeight / 2);//计算控件距离顶部距离
                    item.Width = Convert.ToInt32(itemWidth);//控件宽度，int类型
                    item.Height = Convert.ToInt32(itemHeight);//控件高度  item.Font = new Font(item.Font.Name, float.Parse((pos[4] * Math.Min(scaleX, scaleY)).ToString()));//字体

                }
            }
        }
    }

}

